//
//  YMCheckUserInforListCell.swift
//  YMMedical
//
//  Created by y s on 2023/7/25.
//

import UIKit

public typealias TextChangeBlock = (_ text: String) -> ()
public typealias ChooseClickBlock = () -> ()

@objcMembers class YMCheckUserInforListCell: UITableViewCell {

    lazy var vBg: UIView = {
        let result = UIView()
        result.backgroundColor = STheme.D9;
        result.clipsToBounds = true
        result.layer.cornerRadius = SFitUtil.fit(8)
        return result
    }()
    lazy var lblTitle: UILabel = {
        let result = SThemeUtil.boldLabel("", color: STheme.H_D1, fontSize: 16)
        return result
    }()
    lazy var tfMain: UITextField = {
        let result = UITextField()
        result.textColor = STheme.D3
        result.font = SFitUtil.fitFont(16)
        result.textAlignment = .right
        result.addTarget(self, action: #selector(textFieldDidChangeValue(_:)), for: .editingChanged)
        return result
    }()
    lazy var imgvArrow: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "home_nav_updown_arrow")
        result.isHidden = true
        return result
    }()
    lazy var btnChoose: UIButton = {
        let result = UIButton(type: .custom)
        result.addTarget(self, action: #selector(chooseClickAction(_:)), for: .touchUpInside)
        result.isHidden = true
        return result
    }()
    
    public var keyboardType: UIKeyboardType = .default{
        didSet {
            tfMain.keyboardType = keyboardType
        }
    }
    public var placeholder: String = "" {
        didSet {
            let attr = NSAttributedString(string: placeholder, attributes: [.font:SFitUtil.fitFont(16),.foregroundColor: STheme.D3])
            tfMain.attributedPlaceholder = attr
        }
    }
    public var titleStr: String = "" {
        didSet {
            lblTitle.text = titleStr
            let width = SFitUtil.sizeOf(lblTitle).width+SFitUtil.fit(5)
            lblTitle.snp.remakeConstraints({ make in
                make.centerY.equalToSuperview()
                make.left.equalTo(SFitUtil.fit(15))
                make.width.equalTo(width)
            })
        }
    }
    public var inputText: String = "" {
        didSet {
            tfMain.text = inputText
        }
    }
    public var isChoose: Bool = false {
        didSet {
            tfMain.isEnabled = !isChoose
            imgvArrow.isHidden = !isChoose
            btnChoose.isHidden = !isChoose
            imgvArrow.snp.updateConstraints { make in
                make.width.equalTo(SFitUtil.fit(isChoose ? SFitUtil.fit(24):0))
                make.right.equalTo(SFitUtil.fit(isChoose ? -15:-9));
            }
        }
    }

    
    
    public var textChangeBlock: TextChangeBlock?
    public var chooseBlock: ChooseClickBlock?
    public var maxLenth: Int = 0   //最大长度
    //限制最大值
    public var maxNum: Int = 0
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUI()
    }
    
    
    // MARK: UI
    func setUI() {
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.addSubview(vBg)
        vBg.addSubview(lblTitle)
        vBg.addSubview(tfMain)
        vBg.addSubview(imgvArrow)
        vBg.addSubview(btnChoose)
        
        vBg.snp.makeConstraints { make in
            make.top.bottom.equalTo(0)
            make.left.equalTo(SFitUtil.fit(15))
            make.right.equalTo(SFitUtil.fit(-15))
        }
        lblTitle.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(SFitUtil.fit(15))
        }
        tfMain.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(lblTitle.snp.right).offset(SFitUtil.fit(10))
            make.right.equalTo(imgvArrow.snp.left).offset(SFitUtil.fit(-6))
        }
        imgvArrow.snp.makeConstraints { make in
            make.right.equalTo(SFitUtil.fit(-9))
            make.centerY.equalToSuperview()
            make.width.equalTo(0)
            make.height.equalTo(SFitUtil.fit(24))
        }
        btnChoose.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.top.bottom.equalTo(0)
            make.width.equalTo(SFitUtil.fit(150))
        }
        
    }
    
    public func updateData(_ dict: Dictionary<String, Any>){
        
    }
    
    func textFieldDidChangeValue(_ sender: UITextField) {
        if let string = sender.text as? NSString {
            let text = string.replacingOccurrences(of:  " ", with: "")
            if maxLenth > 0 && text.count > maxLenth {
                sender.text = String(text.prefix(maxLenth))
            }else{
                sender.text = text
            }
        }
        let numText = sender.text ?? "0"
        if maxNum > 0 && Float(numText) ?? 0 > Float(maxNum) {
            sender.text = "\(maxNum)"
        }
        
        if textChangeBlock != nil {
            textChangeBlock!(sender.text ?? "")
        }
    }
    func chooseClickAction(_ sender: UIButton) {
        if chooseBlock != nil {
            chooseBlock!()
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
